package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.unionwar.UnionWarDispatchDetail;
import com.yanqu.road.entity.activity.unionwar.UnionWarUnionJoin;
import com.yanqu.road.entity.enums.eUnionWarWinMark;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_CROSS_UNION_WAR_BATTLE_RESULT_SYNC_FORM_CROSS,desc = "对战结果信息从跨服下发")
public class UnionWarBattleResultSyncCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CrossUnionWarProto.CrossUnionWarBattleResultSendMsg sendMsg = CrossUnionWarProto.CrossUnionWarBattleResultSendMsg.parseFrom(packet.getBytes());
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(sendMsg.getActivityId());
        List<String> unionUidList = new ArrayList<>();
        if(unionWar != null) {
            for (CrossUnionWarProto.CroUnionWarBattleResultTemp temp : sendMsg.getResultList()) {
                UnionWarDispatchDetail detail = unionWar.getUnionWarDispatchDetailList(temp.getUnionUid(), sendMsg.getPhase(), sendMsg.getSession());
//                UnionWarDispatchDetail detail = unionWar.checkAndGetUnionWarDispatchDetails(temp.getUnionUid(), sendMsg.getPhase(), sendMsg.getSession());
                if(detail != null){
                    Object lock = unionWar.getUnionLocker(temp.getUnionUid());
                    synchronized (lock) {
                        detail.setWinMark(temp.getWinMark());
                        detail.setEnemyUnionUid(temp.getEnemyUnionUid());
                        detail.setEnemyUnionInfo(UnionBasePb.parseUnionBaseInfo(temp.getEnemyBaseInfo()));
                        UnionWarUnionJoin unionJoin = unionWar.getUnionWarNameList(detail.getUnionUid());
                        if(unionJoin != null){
                            unionJoin.setPhaseOneScore(temp.getScore1());
                            unionJoin.setPhaseOneRank(temp.getRank1());
                            unionJoin.setPhaseTwoScore(temp.getScore2());
                            unionJoin.setPhaseTwoRank(temp.getRank2());
                        }
                        unionUidList.add(detail.getUnionUid());

                        //阶段结束；锁定商会成员
                        if((sendMsg.getPhase() == 1 && sendMsg.getSession() == unionWar.getAllSession(1))){
                            unionWar.syncCanGetRankRewardMemberMap(detail.getUnionUid(), 1);
                        }else if((sendMsg.getPhase() == 2 && sendMsg.getSession() == unionWar.getAllSession(2))){
                            //unionWar.syncCanGetRankRewardMemberMap(detail.getUnionUid(), 2);
                        }else if(detail.getWinMark() == eUnionWarWinMark.Lose.getValue() && sendMsg.getPhase() == 2){
                            //unionWar.syncCanGetRankRewardMemberMap(detail.getUnionUid(), 2);
                        }
                    }
                }else {
                    unionUidList.add(temp.getUnionUid());
                }
            }
        }
        if(unionUidList.size() > 0){
            CrossUnionWarProto.CroUnionWarBattleResultBackMsg.Builder backMsg = CrossUnionWarProto.CroUnionWarBattleResultBackMsg.newBuilder();
            backMsg.setActivityId(sendMsg.getActivityId());
            backMsg.setPhase(sendMsg.getPhase());
            backMsg.setSession(sendMsg.getSession());
            backMsg.addAllUnionUid(unionUidList);
            backMsg.setGroupId(sendMsg.getGroupId());
            //GamePlayerMgr.sendChannelPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_WAR_BATTLE_RESULT_SYNC_BACK, backMsg));
            GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_UNION_WAR_BATTLE_RESULT_SYNC_BACK, backMsg, 0, sendMsg.getActivityId(), sendMsg.getGroupId());
        }
    }
}
